package org.example.stream;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test01 {


    public static class User {

        private String name;

        private Integer age;


        public User(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) {


        List<String> stringList = Arrays.asList("1", "2", "3", "a", "b", "c");


        //1.forEach
        for (String item : stringList) {

            System.out.println(item);
        }


        //2.iterator

        Iterator<String> iterator = stringList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }


        //1.构造stream


        Stream<String> stringStream = Stream.of("1", "2", "3");

        Stream<String> listStream = stringList.stream();


        //2操作stream

        //过滤
        List<String> res = stringList.stream().filter(it -> it.equals("1")).collect(Collectors.toList());
        System.out.println(res);


        boolean res2 = stringList.stream().anyMatch(it -> it.length() > 2);
        System.out.println(res2);


        Stream<User> userStream = Stream.of(
                new User("tom", 12));


        List<String> strings = userStream.map(User::getName).collect(Collectors.toList());
        System.out.println(strings);

    }
}
